﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//对象池技术[子弹或者炮弹]
public class ObjectPool : MonoBehaviour {
    
    [Header("可以生成的最大数量"),SerializeField()]
    private uint maxNumber;//可以生成的最大数量
    private Dictionary<string, List<GameObject>> gameDic;//用于储存对象的池子（字典）
    private int curNumber;
    void Start()
    {
        gameDic = new Dictionary<string, List<GameObject>>();
    }
    /// <summary>
    /// 实例化对象池并获取其中对象 / 创建对象池的对象和获取
    /// </summary>
    /// <param name="_path">从Resources文件夹中要加载对象名字</param>
    /// <param name="createPos">生成位置</param>
    /// <param name="createRot">旋转位置</param>
    /// <returns>从对象池中获取的对象</returns>
    public GameObject GetObject(GameObject go,Vector3 createPos,Quaternion createRot) {
        GameObject goTemp;
        string name = go.name + "(Clone)";
        if (gameDic.ContainsKey(name) && gameDic[name].Count > 0)
        {
            List<GameObject> arrayList = gameDic[name];
            goTemp = arrayList[0];
            gameDic[name].RemoveAt(0);
            goTemp.SetActive(true);
            goTemp.transform.position = createPos;
            goTemp.transform.rotation = createRot;
        }
        else {
            if (maxNumber > curNumber)
            {
                //goTemp = Instantiate(Resources.Load(prefix + _path),createPos,createRot)as GameObject;
                goTemp = Instantiate(go, createPos,createRot) as GameObject;           
                goTemp.GetComponent<DestroyPoolObject>().objectPool = this;  //统一对象池销毁类(脚本) 第1种 
                    //DecideComponent(goTemp); 第2种  
                curNumber++;
            }
            else {
                return null;
            }            
        }
        return goTemp;
    }
    /// <summary>
    /// 回收不用的对象到对象池中 / 回收对象池的对象
    /// </summary>
    /// <param name="returnGo">需要回收的对象</param>
    /// <returns></returns>
    public GameObject ReturnObjcet(GameObject returnGo) {
        string key = returnGo.name;
        if (gameDic.ContainsKey(key))
        {
            gameDic[key].Add(returnGo);
        }
        else {
            gameDic[key] = new List<GameObject>() { returnGo };
        }
        returnGo.SetActive(false);
        return returnGo;
    }
}
//[Header("(有需要时)前面的文件夹路径")]
//public string prefix;//前面的文件夹路径
//public GameObject GetObject(string _path, Vector3 createPos, Quaternion createRot)
//{
//    GameObject goTemp;
//    string name = _path + "(Clone)";
//    if (gameDic.ContainsKey(name) && gameDic[name].Count > 0)
//    {
//        List<GameObject> arrayList = gameDic[name];
//        goTemp = arrayList[0];
//        gameDic[name].RemoveAt(0);
//        goTemp.SetActive(true);
//        goTemp.transform.position = createPos;
//        goTemp.transform.rotation = createRot;
//    }
//    else
//    {
//        if (maxNumber > curNumber)
//        {
//            //goTemp = Instantiate(Resources.Load(prefix + _path),createPos,createRot)as GameObject;
//            goTemp = Instantiate(Resources.Load(prefix + _path), createPos, createRot) as GameObject;
//            goTemp.GetComponent<DestroyPoolObject>().objectPool = this;  //统一对象池销毁类(脚本) 第1种 
//                                                                         //DecideComponent(goTemp); 第2种  
//            curNumber++;
//        }
//        else
//        {
//            return null;
//        }
//    }
//    return goTemp;
//}


/// <summary>
/// 判断有没有（对象池）销毁类
/// </summary>
/// <param name="thisGo">需要判断的对象</param>
//void DecideComponent(GameObject thisGo) {
//    if (thisGo.GetComponent<DestroyPoolObject>() != null)
//    {
//        thisGo.GetComponent<DestroyPoolObject>().objectPool = this;
//    }
//}